package com.cspg.design.ui.project;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cspg.design.CSPGApplication;
import com.cspg.design.R;
import com.cspg.design.bean.deviceBean.HotLineWorkPlanBean;
import com.cspg.design.dao.gen.DaoSession;
import com.cspg.design.dao.gen.HotLineWorkPlanBeanDao;
import com.cspg.design.presenter.ProjectPresenter;
import com.cspg.design.utils.ActionBarUtil;
import com.cspg.design.utils.StatusBarUtil;
import com.cspg.design.utils.Utils;
import com.cspg.design.widget.ToastUtils;

public class ProjectHotLineWorkActivity extends AppCompatActivity {

    private EditText etNum1;
    private EditText etNum2;
    private EditText etNum3;
    private EditText etNum4;
    private EditText etNum5;
    private EditText etNum6;
    private EditText etNum7;
    private EditText etNum8;
    private EditText etNum9;
    private EditText etNum10;
    private EditText etNum11;
    private EditText etNum12;

    private String projectId;
    DaoSession daoSession;
    private ProjectPresenter projectPresenter;
    private boolean isExist = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_hot_line_work);
        StatusBarUtil.setStatusBarColor(this, R.color.colorPrimary);
        ActionBarUtil.setActionBar(this, R.string.project_hot_line_work);
        daoSession = ((CSPGApplication) getApplication()).getDaoSession();
        projectPresenter = new ProjectPresenter(this);
        projectId = getIntent().getStringExtra("projectId");
          etNum1 = findViewById(R.id.et_hot_line_work_1);
          etNum2 = findViewById(R.id.et_hot_line_work_2);
          etNum3 = findViewById(R.id.et_hot_line_work_3);
          etNum4 = findViewById(R.id.et_hot_line_work_4);
          etNum5 = findViewById(R.id.et_hot_line_work_5);
          etNum6 = findViewById(R.id.et_hot_line_work_6);
          etNum7 = findViewById(R.id.et_hot_line_work_7);
          etNum8 = findViewById(R.id.et_hot_line_work_8);
          etNum9 = findViewById(R.id.et_hot_line_work_9);
          etNum10 = findViewById(R.id.et_hot_line_work_10);
          etNum11 = findViewById(R.id.et_hot_line_work_11);
          etNum12 = findViewById(R.id.et_hot_line_work_12);
        Button btnSave = findViewById(R.id.et_hot_line_work_save);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if (projectId == null || projectId.isEmpty())
                 {
                     ToastUtils.toast(ProjectHotLineWorkActivity.this,"无项目ID号，不能保存");
                 }

                HotLineWorkPlanBean hotLineWorkPlanBean = daoSession.getHotLineWorkPlanBeanDao().queryBuilder().where(HotLineWorkPlanBeanDao.Properties.ProjectId.eq(projectId)).unique() ;
                if (hotLineWorkPlanBean != null)
                {
                    daoSession.getHotLineWorkPlanBeanDao().delete(hotLineWorkPlanBean);
                }
                else {
                    hotLineWorkPlanBean = new HotLineWorkPlanBean();
                }
                 hotLineWorkPlanBean.setProjectId(projectId);
                 String txt1 = etNum1.getText().toString();
                 if (txt1.isEmpty() || !Utils.isNumber(txt1))
                 {
                     txt1 = "0";
                 }
                 int num1 = Integer.parseInt(txt1);
                 hotLineWorkPlanBean.setN1(num1);
                String txt2 = etNum2.getText().toString();
                if (txt2.isEmpty() || !Utils.isNumber(txt2))
                {
                    txt2 = "0";
                }
                int num2 = Integer.parseInt(txt2);
                hotLineWorkPlanBean.setN2(num2);
                String txt3 = etNum3.getText().toString();
                if (txt3.isEmpty() || !Utils.isNumber(txt3))
                {
                    txt3 = "0";
                }
                int num3 = Integer.parseInt(txt3);
                hotLineWorkPlanBean.setN3(num3);
                String txt4 = etNum4.getText().toString();
                if (txt4.isEmpty() || !Utils.isNumber(txt4))
                {
                    txt4 = "0";
                }
                int num4 = Integer.parseInt(txt4);
                hotLineWorkPlanBean.setN4(num4);
                String txt5 = etNum5.getText().toString();
                if (txt5.isEmpty() || !Utils.isNumber(txt5))
                {
                    txt5 = "0";
                }
                int num5 = Integer.parseInt(txt5);
                hotLineWorkPlanBean.setN5(num5);
                String txt6 = etNum6.getText().toString();
                if (txt6.isEmpty() || !Utils.isNumber(txt6))
                {
                    txt6 = "0";
                }
                int num6 = Integer.parseInt(txt6);
                hotLineWorkPlanBean.setN6(num6);
                String txt7 = etNum7.getText().toString();
                if (txt7.isEmpty() || !Utils.isNumber(txt7))
                {
                    txt7 = "0";
                }
                int num7 = Integer.parseInt(txt7);
                hotLineWorkPlanBean.setN7(num7);
                String txt8 = etNum8.getText().toString();
                if (txt8.isEmpty() || !Utils.isNumber(txt8))
                {
                    txt8 = "0";
                }
                int num8 = Integer.parseInt(txt8);
                hotLineWorkPlanBean.setN8(num8);
                String txt9 = etNum9.getText().toString();
                if (txt9.isEmpty() || !Utils.isNumber(txt9))
                {
                    txt9 = "0";
                }
                int num9 = Integer.parseInt(txt9);
                hotLineWorkPlanBean.setN9(num9);
                String txt10 = etNum10.getText().toString();
                if (txt10.isEmpty() || !Utils.isNumber(txt10))
                {
                    txt10 = "0";
                }
                int num10 = Integer.parseInt(txt10);
                hotLineWorkPlanBean.setN10(num10);
                String txt11 = etNum11.getText().toString();
                if (txt11.isEmpty() || !Utils.isNumber(txt11))
                {
                    txt11 = "0";
                }
                int num11 = Integer.parseInt(txt11);
                hotLineWorkPlanBean.setN11(num11);
                String txt12 = etNum12.getText().toString();
                if (txt12.isEmpty() || !Utils.isNumber(txt12))
                {
                    txt12 = "0";
                }
                int num12 = Integer.parseInt(txt12);
                hotLineWorkPlanBean.setN12(num12);

                if (projectId != null){
                    if (isExist)
                    {
                        projectPresenter.updateHotLineWork(hotLineWorkPlanBean);
                    }
                    else {
                        projectPresenter.addHotLineWork(hotLineWorkPlanBean);
                    }
//                            daoSession.getHotLineWorkPlanBeanDao().insertOrReplace(hotLineWorkPlanBean);
                }

            }
        });
        initData();
    }
    private void initData()
    {
        if (projectId == null)
        {
            return;
        }
        HotLineWorkPlanBean hotLineWorkPlanBean = daoSession.getHotLineWorkPlanBeanDao()
                .queryBuilder().where(HotLineWorkPlanBeanDao.Properties.ProjectId.eq(projectId)).unique() ;
        if (hotLineWorkPlanBean != null){
            this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    etNum1.setText(hotLineWorkPlanBean.getN1()+"");
                    etNum2.setText(hotLineWorkPlanBean.getN2()+"");
                    etNum3.setText(hotLineWorkPlanBean.getN3()+"");
                    etNum4.setText(hotLineWorkPlanBean.getN4()+"");
                    etNum5.setText(hotLineWorkPlanBean.getN5()+"");
                    etNum6.setText(hotLineWorkPlanBean.getN6()+"");
                    etNum7.setText(hotLineWorkPlanBean.getN7()+"");
                    etNum8.setText(hotLineWorkPlanBean.getN8()+"");
                    etNum9.setText(hotLineWorkPlanBean.getN9()+"");
                    etNum10.setText(hotLineWorkPlanBean.getN10()+"");
                    etNum11.setText(hotLineWorkPlanBean.getN11()+"");
                    etNum12.setText(hotLineWorkPlanBean.getN12()+"");
                }
            });
        }
        else {
            isExist = false;
        }
    }
}